<?php

namespace App\Http\Controllers;

use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;


use Session;
use Illuminate\Http\Request;

class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
    protected $is_login = 0;    //是否登录
    protected $is_admin = 0;    //是否是管理员
    protected $is_black = 0;    //是否在黑名单中
    protected $uid = 0;

    /**
     * 初始化
     */
    public function __construct()
    {
        date_default_timezone_set('Asia/ShangHai');
        $this->is_login();
    }

    /**
     * 判断用户是否登录
     * @return boolean
     */
    private function is_login()
    {
        if (Session::has('uid')) {

            $this->is_login = 1;
            $this->uid = Session::get('uid');

            $user = $this->get_user($this->uid);
            if ($user->is_admin) {
                $this->is_admin = 1;
            }
            if (empty($user->status)) {
                $this->is_black = 1;
            }
        }
    }

    /**
     * 获取某个用户信息
     * @param  integer uid
     */
    protected function get_user($uid = 0)
    {
        return \App\User::where('id', $uid)->first();
    }

    /**
     * 获取所有留言
     */
    public function get_comments()
    {
        //return DB::table('comment')
        //  \App\Comment::find(1)->user->user_name;
        return \App\Comment::select('user_name', 'text', 'comment.create_time')
                ->leftJoin('user', 'user.id', '=', 'comment.uid')
                ->orderBy('create_time', 'desc')
                ->paginate(10);
                //->get();
    }
}
